package hos.http;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * <p>Title: JsonUtils </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-04-03 22:05
 */
public class JsonUtils {

    public static JSONObject orJSONObject(JSONObject jsonObject, String name, JSONObject defaultValue) {
        try {
            JSONObject object = jsonObject.optJSONObject(name);
            if (object == null) {
                return defaultValue;
            }
            return object;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static JSONArray orJSONArray(JSONObject jsonObject, String name, JSONArray defaultValue) {
        try {
            JSONArray array = jsonObject.optJSONArray(name);
            if (array == null) {
                return defaultValue;
            }
            return array;
        } catch (Exception e) {
            e.printStackTrace();
            return defaultValue;
        }
    }

    public static JSONObject orJSONObject(JSONObject jsonObject, String name) {
        return orJSONObject(jsonObject, name, new JSONObject());
    }

    public static JSONArray orJSONArray(JSONObject jsonObject, String name) {
        return orJSONArray(jsonObject, name, new JSONArray());
    }

    /**
     * @noinspection unchecked
     */
    public static String toJson(List<?> list) {
        if (list == null || list.isEmpty()) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (Object newList : list) {
            if (newList instanceof List) {
                String json = toJson((List<?>) newList);
                sb.append("[").append(json).append("]").append(",");
            } else if (newList instanceof Map) {
                String json = toJson((Map<String, ?>) newList);
                sb.append(json).append(",");
            } else if (newList instanceof HttpParam) {
                String json = ((HttpParam) newList).toJson();
                sb.append(json).append(",");
            } else {
                sb.append("\"").append(newList).append("\"").append(",");
            }
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
//        Object root = list.get(0);
//        if (root instanceof List) {
//            for (Object newList : list) {
//                String json = toJson((List<?>) newList);
//                sb.append("[").append(json).append("]").append(",");
//            }
//            sb.deleteCharAt(sb.lastIndexOf(","));
//        } else if (root instanceof Map) {
//            for (Object mapList : list) {
//                String json = toJson((Map<String, ?>) mapList);
//                sb.append(json);
//            }
//        } else {
//            for (Object object : list) {
//                sb.append("'").append(object).append("'").append(",");
//            }
//            sb.deleteCharAt(sb.lastIndexOf(","));
//        }
        sb.append("]");
        return sb.toString();
    }


    /**
     * @noinspection unchecked
     */
    public static String toJson(Map<String, ?> mapList) {
        if (mapList == null || mapList.isEmpty()) {
            return "{}";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (String key : mapList.keySet()) {
            Object object = mapList.get(key);
            Object value;
            if (object instanceof List) {
                value = toJson((List<?>) object);
            } else if (object instanceof Map) {
                value = toJson((Map<String, ?>) object);
            } else if (object instanceof HttpParam) {
                value = ((HttpParam) object).toJson();
            } else {
                value = object;
            }
            if (value != null) {
                sb.append("\"").append(key).append("\":")
                        .append("\"").append(value).append("\",");
            } else {
                sb.append("\"").append(key).append("\":")
                        .append("\"").append("").append("\",");
            }
        }
        sb.deleteCharAt(sb.lastIndexOf(","));
        sb.append("}");
        return sb.toString();
    }

    public static String toFileJson(List<HttpFile> fileList) {
        if (fileList == null || fileList.isEmpty()) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        for (HttpFile httpFile : fileList) {
            if (builder.length() != 0) {
                builder.append(",");
            }
            builder.append(httpFile.toJson());
        }
        return builder.toString();
    }

    public static String toDownloadJson(HttpRequestDownload download) {
        return "{\"fileName\":\"" + download.getFileName() + "\"," +
                "\"fileDir\":\"" + download.getFileDir() + "\"," +
                "\"isMD5Verify\":\"" + download.isMD5Verify() + "\"," +
                "\"isMD5Verify\":\"" + download.isMD5Verify() + "\"," +
                "\"isTempFile\":\"" + download.isTempFile() + "\"," +
                "\"isFileNameDecode\":\"" + download.isFileNameDecode() + "\"," +
                "\"conflictRename\":\"" + download.isConflictRename() + "\"}";
    }
}
